# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'Yang'
__mtime__ = '2019/1/17 20:06'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
import requests
import json

url = 'https://btshow.jd.com/queryBtPlanInfo.do?callback=queryBtPlanInfo&sku=100000820311&cId=9987%2C653%2C655&num=1&amount=6498&sourceType=PC-XQ&shopId=116240&ver=1&areaId=1&isJd=true&_=1547726600819'
headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7',
    'Connection': 'keep-alive',
    'Host': 'btshow.jd.com',
    'Referer': 'https://item.jd.com/34594345130.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

}

headers_shop = {
    'authority': 'mall.jd.com',
    'method': 'POST',
    'path': '/shopLevel-624094.html',
    'scheme': 'https',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7',
    'cache-control': 'max-age=0',
    'content-length': '15',
    'content-type': 'application/x-www-form-urlencoded',
    'origin': 'https://mall.jd.com',
    'referer': 'https://mall.jd.com/shopLevel-624094.html',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}

url = 'https://search.jd.com/Search?keyword=%E5%8D%8E%E4%B8%BAmate20&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&suggest=3.def.0.V10&wq=%E5%8D%8E%E4%B8%BA&ev=exbrand_%E5%8D%8E%E4%B8%BA%EF%BC%88HUAWEI%EF%BC%89%5E&page=1&s=1&click=0'

headers = {
    'authority': 'search.jd.com',
    'method': 'POST',
    'path': '/Search?keyword=%E5%8D%8E%E4%B8%BAmate20&enc=utf-8&wq=%E5%8D%8E%E4%B8%BAmate20&pvid=34e7d898da614354954657e5dae76f3e',
    'scheme': 'https',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7',
    'referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fsearch.jd.com%2FSearch%3Fkeyword%3D%25E5%258D%258E%25E4%25B8%25BAmate20%26enc%3Dutf-8%26wq%3D%25E5%258D%258E%25E4%25B8%25BAmate20%26pvid%3D34e7d898da614354954657e5dae76f3e',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}

import re
from bs4 import BeautifulSoup

url = 'https://search.jd.com/Search?keyword=%E5%8D%8E%E4%B8%BAmate20&enc=utf-8&suggest=1.his.0.0&wq=&pvid=36886887c0994b34949a6c58a1c05c93'

# page = requests.get(url, headers=headers)
# page.encoding = page.apparent_encoding
# soup = BeautifulSoup(page.text, 'lxml')
# soup.select('#J_scroll_loading')
# print(json.loads(page.text[len('queryBtPlanInfo()') - 1:-1]))
rex = re.compile('<li.class="gl-item".data-sku=".*?".data-spu=".*?".data-pid="(.*?)"')
# print(soup.select('#J_scroll_loading'))
print(re.findall('< div class ="star-gray" title="(.*?)"', '< div class ="star-gray" title="3.5" >'))

# s = '<a target="_blank" class="curr-shop"  href="//mall.jd.com/index-1000004259.html" title="华为京东自营官方旗舰店">华为京东自营官方旗舰店</a>'
# pro_name_list
# rex = re.compile('<a.target="_blank".class="curr-shop".href=".*?".title="(.*?)"')
# pro_name_list = re.findall(rex, s)
# print(pro_name_list)

json_ = "{'isDiscountAll': True, 'isBtUser': False, 'isShopAva': True, 'isAva': True, 'isLogin': False, 'ver': '1', 'isItemAva': True, 'noAvaInfo': '', 'marketingText': '白条支付即享6期免息/12期息费五折', 'result': {'isSuccess': True, 'info': 'success', 'code': '00000'}, 'creditStatus': 2, 'planInfos': [{'isMaxDiscount': False, 'isDiscount': True, 'firstPay': 6498.0, 'plan': 1, 'laterPay': 6498.0, 'total': 6498.0, 'curTotal': 6498.0, 'discountList': [{'activityCode': '00001', 'couponInfo': '白条支付即享6期免息/12期息费五折', 'discountAmount': '0.00', 'activityType': 'MX', 'couponCode': '00001'}], 'fee': 0.0, 'firstRepayDate': '2019-02-17', 'rate': 0.0, 'planFee': 0.0}, {'isMaxDiscount': False, 'isDiscount': True, 'firstPay': 2166.0, 'plan': 3, 'laterPay': 2166.0, 'total': 6498.0, 'curTotal': 2166.0, 'discountList': [{'activityCode': '00001', 'couponInfo': '白条支付即享6期免息/12期息费五折', 'discountAmount': '0.00', 'activityType': 'MX', 'couponCode': '00001'}], 'fee': 0.0, 'firstRepayDate': '2019-02-17', 'rate': 0.0, 'planFee': 0.0}, {'isMaxDiscount': True, 'isDiscount': True, 'firstPay': 1083.0, 'plan': 6, 'laterPay': 1083.0, 'total': 6498.0, 'curTotal': 1083.0, 'discountList': [{'activityCode': '00001', 'couponInfo': '白条支付即享6期免息/12期息费五折', 'discountAmount': '0.00', 'activityType': 'MX', 'couponCode': '00001'}], 'fee': 0.0, 'firstRepayDate': '2019-02-17', 'rate': 0.0, 'planFee': 0.0}, {'isMaxDiscount': False, 'isDiscount': True, 'firstPay': 557.75, 'plan': 12, 'laterPay': 557.75, 'total': 6693.0, 'text': '起', 'curTotal': 557.75, 'discountList': [{'activityCode': '00001', 'couponInfo': '白条支付即享6期免息/12期息费五折', 'discountAmount': '50.00', 'activityType': 'MX', 'couponCode': '00001'}], 'fee': 195.0, 'firstRepayDate': '2019-02-17', 'rate': 0.25, 'planFee': 16.25}, {'isMaxDiscount': False, 'isDiscount': True, 'firstPay': 303.24, 'plan': 24, 'laterPay': 303.24, 'total': 7277.76, 'text': '起', 'curTotal': 303.24, 'discountList': [{'activityCode': '00001', 'couponInfo': '白条支付即享6期免息/12期息费五折', 'discountAmount': '100.00', 'activityType': 'MX', 'couponCode': '00001'}], 'fee': 779.76, 'firstRepayDate': '2019-02-17', 'rate': 0.5, 'planFee': 32.49}], 'key': '2126F62699C4839809FCEBB226CF7226', 'isFull': False, 'isSkuAva': True}"
json_ = '{"planInfos":[{"fee":0.00,"planFee":0.00,"firstPay":6498.00,"laterPay":6498.00,"total":6498.00,"curTotal":6498.00,"plan":1,"rate":0.00,"firstRepayDate":"2019-02-17","isDiscount":true,"discountList":[{"activityCode":"00001","couponCode":"00001","discountAmount":"0.00","couponInfo":"白条支付即享6期免息/12期息费五折","activityType":"MX"}],"isMaxDiscount":false},{"fee":0.00,"planFee":0.00,"firstPay":2166.00,"laterPay":2166.00,"total":6498.00,"curTotal":2166.00,"plan":3,"rate":0.00,"firstRepayDate":"2019-02-17","isDiscount":true,"discountList":[{"activityCode":"00001","couponCode":"00001","discountAmount":"0.00","couponInfo":"白条支付即享6期免息/12期息费五折","activityType":"MX"}],"isMaxDiscount":false},{"fee":0.00,"planFee":0.00,"firstPay":1083.00,"laterPay":1083.00,"total":6498.00,"curTotal":1083.00,"plan":6,"rate":0.00,"firstRepayDate":"2019-02-17","isDiscount":true,"discountList":[{"activityCode":"00001","couponCode":"00001","discountAmount":"0.00","couponInfo":"白条支付即享6期免息/12期息费五折","activityType":"MX"}],"isMaxDiscount":true},{"fee":195.00,"planFee":16.25,"firstPay":557.75,"laterPay":557.75,"total":6693.00,"curTotal":557.75,"plan":12,"rate":0.25,"firstRepayDate":"2019-02-17","isDiscount":true,"discountList":[{"activityCode":"00001","couponCode":"00001","discountAmount":"50.00","couponInfo":"白条支付即享6期免息/12期息费五折","activityType":"MX"}],"text":"起","isMaxDiscount":false},{"fee":779.76,"planFee":32.49,"firstPay":303.24,"laterPay":303.24,"total":7277.76,"curTotal":303.24,"plan":24,"rate":0.50,"firstRepayDate":"2019-02-17","isDiscount":true,"discountList":[{"activityCode":"00001","couponCode":"00001","discountAmount":"100.00","couponInfo":"白条支付即享6期免息/12期息费五折","activityType":"MX"}],"text":"起","isMaxDiscount":false}],"creditStatus":2,"isDiscountAll":true,"isLogin":false,"isBtUser":false,"isFull":false,"isAva":true,"isShopAva":true,"isSkuAva":true,"isItemAva":true,"noAvaInfo":"","key":"09DD929F3FEFDADE8274F8AEC90C210F","ver":"1","marketingText":"白条支付即享6期免息/12期息费五折","result":{"isSuccess":true,"code":"00000","info":"success"}}'

# json_ = json.loads(json_)
# # print(json_['planInfos'])
# for k in json_['planInfos']:
#     if k['plan'] == 1:
#         print(k['firstPay'])
#     print()


head = {'authority': 'search.jd.com',
        'method': 'GET',
        'path': '/s_new.php?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA',
        'scheme': 'https',
        'referer': 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=3&s=58&click=0',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
        'Cookie': 'qrsc=3; pinId=RAGa4xMoVrs; xtest=1210.cf6b6759; ipLocation=%u5E7F%u4E1C; _jrda=5; TrackID=1aUdbc9HHS2MdEzabuYEyED1iDJaLWwBAfGBfyIHJZCLWKfWaB_KHKIMX9Vj9_2wUakxuSLAO9AFtB2U0SsAD-mXIh5rIfuDiSHSNhZcsJvg; shshshfpa=17943c91-d534-104f-a035-6e1719740bb6-1525571955; shshshfpb=2f200f7c5265e4af999b95b20d90e6618559f7251020a80ea1aee61500; cn=0; 3AB9D23F7A4B3C9B=QFOFIDQSIC7TZDQ7U4RPNYNFQN7S26SFCQQGTC3YU5UZQJZUBNPEXMX7O3R7SIRBTTJ72AXC4S3IJ46ESBLTNHD37U; ipLoc-djd=19-1607-3638-3638.608841570; __jdu=930036140; user-key=31a7628c-a9b2-44b0-8147-f10a9e597d6f; areaId=19; __jdv=122270672|direct|-|none|-|1529893590075; PCSYCityID=25; mt_xid=V2_52007VwsQU1xaVVoaSClUA2YLEAdbWk5YSk9MQAA0BBZOVQ0ADwNLGlUAZwQXVQpaAlkvShhcDHsCFU5eXENaGkIZWg5nAyJQbVhiWR9BGlUNZwoWYl1dVF0%3D; __jdc=122270672; shshshfp=72ec41b59960ea9a26956307465948f6; rkv=V0700; __jda=122270672.930036140.-.1529979524.1529984840.85; __jdb=122270672.1.930036140|85.1529984840; shshshsID=f797fbad20f4e576e9c30d1c381ecbb1_1_1529984840145'
        }
import time
u = 'https://search.jd.com/s_new.php?keyword=%E5%8D%8E%E4%B8%BAmate20&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&suggest=1.his.0.0&ev=exbrand_%E5%8D%8E%E4%B8%BA%EF%BC%88HUAWEI%EF%BC%89%5E&page={0}&s={1}&scrolling=y&log_id={2}&tpl=3_M'
u = u.format((29 * 2 - 1), (29 * 60 - 60), ('%0.5f' % time.time()))
print(u)
page = requests.get(u, headers=head)
print(page.is_redirect)
print(page.is_permanent_redirect)
print(page.text)
